﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
void SLTPrint(SLTNode* phead) {
	//形参是plist,实参是phead
	SLTNode* pcur = phead;
	while (pcur)
	{
		printf("%d->",pcur->data);
		pcur = pcur->next;
	}
	printf("NULL\n");
}
//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x) {
	assert(pphead);
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
		if (newnode==NULL) {
			perror("malloc fail!");
			exit(1);
		}
		else
		{
			newnode->data = x;
			newnode->next = NULL;
		}
		

	//一个结点也没有
	if (*pphead==NULL) {
		*pphead=newnode;
	}
	else
	{
		SLTNode* ptail = *pphead;
		while (ptail ->next)
		{
			ptail = ptail->next;
		}
		ptail->next=newnode;
	}
	
}
//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x) {
	assert(pphead);
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL) {
		perror("malloc fail!");
		exit(1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	//一个结点也没有
	
		newnode->next=*pphead;
		*pphead=newnode;
	
}
//尾删
void SLTPopBack(SLTNode** pphead) {
	assert(pphead&&*pphead);
	//只有一个结点的情况
	if ((*pphead)->next==NULL) {
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* ptail = *pphead;
		SLTNode* prev = NULL;
		while (ptail->next)
		{
			prev=ptail;
			ptail = ptail->next;
		}
		prev->next = NULL;
		free(ptail);
		ptail = NULL;
	}

}
//头删
void SLTPopFront(SLTNode** pphead) {
	assert(*pphead);
	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead=next;
}
//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x) {
	assert(phead);
	SLTNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data==x) {
			return pcur;
		
		}
		pcur = pcur->next;
	}
	//找不到
	return NULL;
}
//在指定位置前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x) {
	assert(pos);
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL) {
		perror("malloc fail!");
		exit(1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	SLTNode* prev = *pphead;
	while (prev->next!=pos)
	{
		prev = prev->next;
	}
	newnode->next=pos;
	prev->next=newnode;
}
//在指定位置之后插⼊数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x) {
	assert(pos&&pos->next);
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL) {
		perror("malloc fail!");
		exit(1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	newnode->next=pos->next;
	pos->next=newnode;
}
//删除pos结点
void SLTErase(SLTNode** pphead, SLTNode* pos) {
	assert(pphead&&*pphead);
	//pos就是头结点
	if (pos==*pphead) {
		SLTPopFront(pphead);
	}
	else
	{
//prev
		SLTNode* prev = *pphead;
		while (prev->next!=pos)
		{
			prev = prev->next;
		}
		//prev pos pos->next
		prev->next=pos->next;
		free(pos);
		pos = NULL;
	}
}
//删除pos之后的结点
void SLTEraseAfter(SLTNode* pos) {
	assert(pos&&pos->next);
	// pos del del->next
	SLTNode* del = pos -> next;
	pos->next=del->next;
	free(del);
	del = NULL;
}
//销毁链表
void SListDestroy(SLTNode** pphead) {
	assert(pphead);
	SLTNode* pcur = *pphead;
	while (pcur)
	{
		SLTNode* next = pcur->next;
		free(pcur);
		pcur=next;
	}
	*pphead = NULL;
}